Linear Search
Here is a description of linear search:
- Starting Conditions: You have an array of items and a target.
- Goal: Find the target in the array of items, or report if
the target is not present.
- Procedure: Look through the slots of the array one by one, starting
with the first slot and ending when either the target is found or
every slot has been examined.
- Result: Where the target was found, or a indication that it was not found.
- For this example: Return the index where the target was found, or -1 if the
target was not found.
Look over the array of Strings.
Apply the algorithm.
Is "Peoria" in the array?
Is "Albany" in the array?
Is "Mystic" in the array?
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
Boston |
Albany |
Detroit |
Phoenix |
Peoria |
Albany |
Houston |
Hartford |